<template>
  <div class="city-list" ref="wrapper">
    <div class="box">
      <div class="area">
        <h2 class="title border-topbottom">当前城市</h2>
        <div class="button-list">
          <div class="button-wrapper">
            <button class="button">{{this.city}}</button>
          </div>
        </div>
      </div>
      <div class="area">
        <h2 class="title border-topbottom">热门城市</h2>
        <div class="button-list">
          <div class="button-wrapper" v-for="city in hotCities" :key="city.id">
            <button class="button" @click="choiceCity(city.name)">{{city.name}}</button>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <h2 class="title border-topbottom">{{key}}</h2>
        <ul class="item-list">
          <li
            class="item border-bottom"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click.prevent="choiceCity(innerItem.name)"
          >{{innerItem.name}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'city-list',
  props: {
    cities: Object,
    hotCities: Array,
    changeAlpha: String
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    choiceCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.$nextTick(() => {
      this.listScroll = new Bscroll(this.$refs.wrapper)
    })
  },
  watch: {
    changeAlpha () {
      let element = this.$refs[this.changeAlpha][0]
      this.listScroll.scrollToElement(element)
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color #ccc
    &:after
      border-color #ccc
  .border-bottom
    &:after
      border-color #ccc
  .city-list
    position absolute
    left 0
    top 1.58rem
    right 0
    bottom 0
    overflow hidden
    .title
      padding-left .2rem
      line-height .44rem
      font-size .26rem
      background-color #eee
      color #666
    .button-list
      box-sizing border-box
      display flex
      overflow hidden
      padding .1rem .6rem .1rem .1rem
      flex-wrap wrap
      width 100%
      .button-wrapper
        float left
        box-sizing border-box
        padding .1rem
        width 33.33%
        .button
          box-sizing border-box
          padding .1rem 0
          width 100%
          text-align center
          border .02rem solid #ccc
          border-radius .06rem
          background-color #fff
    .item-list
      .item
        padding .2rem
        line-height .2rem
</style>
